#
# @author Wójcik Karol <wojcikk@student.agh.edu.pl>
# @licence http://www.gnu.org/copyleft/gpl.html GNU/GPL
#

class LessonsUsersController < ApplicationController
layout 'standard'
before_filter :login_required, :authorize
 
  def authorize
    if session['user'].role != "admin"
        redirect_back_or_default :controller => "error", :action => "info"
    end
  end

  def index
    list
    render :action => 'list'
  end

  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def list
    #@lessons_user_pages, @lessons_users = paginate :lessons_users, :per_page => 10
    @users = User.find(:all, :conditions => "role = 'prowadzacy'", :order => "login")
    sortby = params['sortby']
    case sortby
    when "user"
	@lessons_users = LessonsUser.find(:all, :order => "user_id")
    when "lesson"
	@lessons_users = LessonsUser.find_by_sql("SELECT * FROM lessons_users 
						    LEFT JOIN lessons ON lessons_users.lesson_id = lessons.id 
						    LEFT JOIN subjects ON lessons.subject_id = subjects.id 
						    ORDER BY subjects.name")
    when "lgrroz"
	@lessons_users = LessonsUser.find(:all, :order => "lgrroz")
    when "lgrzaj"
	@lessons_users = LessonsUser.find(:all, :order => "lgrzaj")
    else
	@lessons_users = LessonsUser.find(:all)
    end 
  end
  
  def list_for_lesson
    
    @users = User.find(:all)
    @lessons = Lesson.find(:all, :conditions => ["subject_id = ? ", params[:id]])
    
    @lessons_users = []
    @lessons.each do |l|
       @lessons_users = @lessons_users + LessonsUser.find(:all, :conditions => ["lesson_id = ?", l.id])
    end
   
    
  end
  
  def user_list
    @lessons_user_pages, @lessons_users = paginate :lessons_users, :per_page => 10
    @users = User.find(:all, :conditions => "role = 'prowadzacy'")
    @lessons_users = LessonsUser.find(:all, :conditions => [ "user_id = ?" , params[:id]])
  end
  
  def show
    @lessons_user = LessonsUser.find(params[:id])
    @users = User.find(:all, :conditions => "role = 'prowadzacy'")
  end

  def create
    @lessons = Lesson.find(:all)
    @users = User.find(:all, :conditions => "role = 'prowadzacy'")
    @lessons_user = LessonsUser.new(params[:lessons_user])
    if @lessons_user.save
      flash[:notice] = 'LessonsUser was successfully created.'
      redirect_to :action => 'ascribe', :id => params[:id]
    else
      render :action => 'assign', :id => params[:id]
    end
  end

  def edit
    @lesson = params[:id]
    @lessons_user = LessonsUser.find(params[:id])
    @users = User.find(:all, :conditions => "role = 'prowadzacy'")
  end

  def update
    back = params['back']
    @lessons_user = LessonsUser.find(params[:id])
    @users = User.find(:all, :conditions => "role = 'prowadzacy'")
    if @lessons_user.update_attributes(params[:lessons_user])
      flash[:notice] = 'LessonsUser was successfully updated.'
      redirect_to :action => 'ascribe', :id => back
    else
      render :action => 'edit'
    end
  end

  def destroy
    LessonsUser.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
  def new
    @users = User.find(:all, :conditions => "role = 'prowadzacy'", :order => "login")
    #@lessons = Lesson.find(:all, :joins=>"INNER JOIN subjects on lessons.subject_id = subjects.id", :order => "subjects.name")
    @lessons = Lesson.find(:all)
    #@lessons = Lesson.find("SELECT * FROM lessons, subjects WHERE lessons.subject_id = subjects.id")
  
  end
  def ascribe
    @users = User.find(:all, :conditions => "role = 'prowadzacy'", :order => "login")
    @lessons = Lesson.find(:all, :conditions => ["id = ?", params[:id]])
    @lessons_users = LessonsUser.find(:all, :conditions => ["lessons_users.lesson_id = ?", params[:id]]);
  end
  def assign
    @lesson = params[:id]
    @lessons_user = LessonsUser.new
    @users = User.find(:all, :conditions => "role = 'prowadzacy'")    
  end
  
end
